package com.xycake.controller.admin;

import com.xycake.entity.Goods;
import com.xycake.entity.Menu;
import com.xycake.entity.Order;
import com.xycake.entity.User;
import com.xycake.service.GoodsService;
import com.xycake.service.MainService;
import com.xycake.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/main")
public class MainController {

    @Autowired
    private MainService mainService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;


    @RequestMapping("/init")
    public Map<String, Object> init(HttpSession httpSession) {
        User loginUser = (User) httpSession.getAttribute("loginUser");
        Map<String, Object> map = new HashMap<>();
        map.put("showName", loginUser.getShowName());
        map.put("menus", mainService.selectMenu(loginUser.getId()));
        return map;
    }

//    @RequestMapping("/role_list")
//    public Map<String, Object> selectWithMenus() {
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 0);
//        map.put("data", mainService.selectRoleWithMenus());
//        return map;
//    }

    @RequestMapping("/menus_title")
    public List<Menu> selectAllTitle() {
        return mainService.selectAllMenuTitle();
    }

//    @RequestMapping("/role_menus_edit")
//    public Map<String, Object> roleMenusEdit(int role_id, int[] auths) {
//        mainService.updataRoleMenus(role_id, auths);
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 1);
//        return map;
//    }

    @RequestMapping("/user_list")
    public Map<String, Object> userList(HttpSession httpSession) {
        User loginUser = (User) httpSession.getAttribute("loginUser");
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", mainService.selectUserList(loginUser.getUsername()));
        return map;
    }

    @RequestMapping("/user_list_edit")
    public Map<String, Object> userListEdit(HttpSession httpSession) {
        User loginUser = (User) httpSession.getAttribute("loginUser");
        Map<String, Object> map = new HashMap<>();
//        mainService.updateUserListEdit(loginUser);
        map.put("code", 1);
        map.put("data", mainService.selectUserList(loginUser.getUsername()));
        return map;
    }


//    public Map<String ,Object> reg(){
//        Map<String ,Object> map=new HashMap<>();
//        return map;
//    }


//    @RequestMapping("/allorder")
//    public List<Order> allOrdergl(){
//        return orderService.selectAllOrder();
//    }

    @RequestMapping("/allorder")
    public Map<String, Object> allOrdergl() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", orderService.selectAllOrder());
        return map;
    }

    @RequestMapping("/uporder")
    public void updataOrder(String ptime,Integer id) {
//        Order order=new Order();
//        order.setPtime(ptime);
        orderService.updataOrder(ptime,id);
    }

    @RequestMapping("/allgoods")
    public Map<String, Object> selectAllGoods() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", goodsService.selectAllGoods());
        return map;
    }

    @RequestMapping("/delgoods")
    public Map<String, Object> delGoods(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", goodsService.selectAllGoods());
        goodsService.delGoodsById(id);
        return map;
    }

    @RequestMapping("/allgoodsid")
    public Map<String, Object> allgoodsid(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", goodsService.selectGoodsById(id));
        return map;
    }

    @RequestMapping("/upimgbyid")
    public void upimgById(Integer id,String img) {
        Goods goods=new Goods();
        goods.setId(id);
        goods.setImg(img);
        goodsService.upimgById(goods);
    }

    @RequestMapping("/updatagoods")
    public void updataGoods(Integer id,String cakename,Integer price,String des) {
        Goods goods=new Goods();
        goods.setId(id);
        goods.setCakename(cakename);
        goods.setPrice(price);
        goods.setDes(des);
        goodsService.updataGoods(goods);
    }

    @RequestMapping("/insertgoods")
    public void insertGoods(String cakename,Integer price,String des,String img) {
        Goods goods=new Goods();
        goods.setCakename(cakename);
        goods.setImg(img);
        goods.setPrice(price);
        goods.setDes(des);
        goods.setDelMark(0);
        goods.setPid(3);
        goods.setQuantity(1);
        goods.setTitle("先生");
        goodsService.insertGoods(goods);
    }

}
